package com.shihb.homework

/**
 * Description:单词计数
 *
 * @author shihb
 * @date 2020/10/3 14:17
 */
object WordCount {
  def main(args: Array[String]): Unit = {
    /*
    val lines =List("hello world","hello scala","hello spark" )
    使用映射集合，list 中单词出现的次数，并按照次数排次序
     */
    val lines =List("hello world","hello scala","hello spark")
    val tuples: List[(String, Int)] = lines.flatMap(_.split(" ")).map((_, 1))
    println(tuples)
    //分组
    val stringToTuples: Map[String, List[(String, Int)]] = tuples.groupBy(_._1)
    println(stringToTuples)
    //计数
    val stringToInt: Map[String, Int] = stringToTuples.map(x => (x._1, x._2.size))
    println(stringToInt)
    //排序
    val result: List[(String, Int)] = stringToInt.toList.sortBy(_._2).reverse
    println(result)



  }

}
